﻿using Utilities.Log;
using Wx.Core;
using Wx.Model;
using Newtonsoft.Json;
namespace Wx.Api
{
    public static class User
    {
        private static LogHelper log = new LogHelper(typeof(User));

        #region Group
        public static string CreateGroup(string name)
        {
            string url = string.Format(Config.GetApiUrl("creategroup"), Token.AccessToken);
            string param = string.Format("{\"\":{\"name\":\"{0}\"}}", name);
            HttpResult result = HttpUtility.Post(url, param);
            if (result.status)
            {
                if (result.msg.Contains("errcode"))
                {
                    log.error("Create Group Error!Error Msg:" + result.msg);
                    return string.Empty;
                }
                else
                {
                    log.info("Create Group OK!Group Name:" + result.msg);
                    return result.msg;
                }
            }
            else
            {
                log.error("Create Group Error!Error Msg:" + result.msg);
                return string.Empty;
            }
        }

        #endregion

        #region User
        public static Model.User.User GetUserMsg(string openId, string token = null)
        {
            string url = string.Format(Config.GetApiUrl("getusermsg"), token != null ? token : Token.AccessToken, openId, "zh_CN");
            HttpResult result = HttpUtility.Get(url);
            if (result.status)
            {
                if (result.msg.Contains("errcode"))
                {
                    log.error("Get User Message Error!Error Msg:" + result.msg);
                    return default(Model.User.User);
                }
                else
                {
                    log.info("Get User Message OK!");
                    return JsonConvert.DeserializeObject<Model.User.User>(result.msg);
                }
            }
            else
            {
                log.error("Get User Message Error!Error Msg:" + result.msg);
                return default(Model.User.User);
            }
        }
        #endregion
    }
}
